<?php
namespace Mobile\Controller;
use \Think\Log;

class ProfitController extends MobileController {
    /**
    * 获取用户收益信息
    * 
    */
    public function index($car_id){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        if(empty($car_id)){
            $this->err('请指定车辆！');
        }
        
        $modelOrder = M('Order');
        
        //因为已经结束的也要算在金额内
        $res = $modelOrder->query("select sum(n_amt) as amt,sum(n_frozen_amt) as frozen_amt from __TABLE__ where uid={$this->uid} and status>=2");
        if($res){
            $freeze_money = $res[0]['frozen_amt'];
            $cash_money = $res[0]['amt'];
        }else{
            $freeze_money = 0;
            $cash_money = 0;
        }
        $total_money = $freeze_money + $cash_money;
                               
        //查找车辆
        unset($where);
        $where['uid'] = $this->uid;
        $where['car_id'] = $car_id;
        $res = M('UserCar')->where($where)->find();
        if($res){
            $c_lcn_no = $res['c_lcn_no'];
            unset($where);
            $where['uid'] = $this->uid;
            $where['car_no'] = $c_lcn_no;
            $where['status'] = array('egt',2);//已支付且已经生成保单的订单,或过期的
            $row = $modelOrder->where($where)->order('order_id desc')->find();
            if($row){
                if(!empty($row['obdid'])){
                    $modelObdDayLog = M('ObdDayLog');
                    unset($where);
                    $where['obdid'] = $row['obdid'];
                    $where['pdate'] = array('between',array($row['start_date'],$row['end_date']));
                    $where['car_status'] = 1;
                    $row['run_num'] = $modelObdDayLog->where($where)->count();
                    $where['car_status'] = 0;
                    $row['stop_num'] = $modelObdDayLog->where($where)->count();
                }
            }
        
            $ret = array(
                'freeze_money' => $freeze_money,
                'cash_money' => $cash_money,
                'total_money' => $total_money,
                'order' => $row,
            );
            $this->succ($ret);
        }else{
            $this->err('找不到指定的车辆！');
        }
    }
    
    /**
    * 车币查询
    * 
    */
    public function getInfo(){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }

        $modelOrder = M('Order');
        
        //因为已经结束的也要算在金额内
        $res = $modelOrder->query("select sum(n_amt) as amt,sum(n_frozen_amt) as frozen_amt from __TABLE__ where uid={$this->uid} and status>=2");
        if($res){
            $freeze_money = $res[0]['frozen_amt'];
            $cash_money = $res[0]['amt'];
        }else{
            $freeze_money = 0;
            $cash_money = 0;
        }
        $total_money = $freeze_money + $cash_money;

        //统计当年的进账
        unset($where);
        $where['uid'] = $this->uid;
        $where['status'] = array('egt',2);
        $subQuery = $modelOrder->field('order_id')->where($where)->select(false);
        $modelLog = M('OrderAmtLog');
        unset($where);
        $where['order_id'] = array('exp',"in $subQuery");
        $where['pdate'] = array('egt',date('Y').'-01-01');
        $where['isfrozen'] = 0;
        $where['ptype'] = 1;
        $in_money = $modelLog->where($where)->sum('chgval');
        $ret = array(
            'freeze_money' => $freeze_money,
            'cash_money' => $cash_money,
            'total_money' => $total_money,
            'year_in' => $in_money,
        );
        $this->succ($ret);
    }
    
    /**
    * 图表页面数据显示
    * 
    */
    public function chart(){
        $uid = $this->uid;
        if(empty($uid)){
            $this->err('用户未登录!');
        }
        $modelCic = M('CicOrder');
        $modelOrder = M('Order');
        
        //因为已经结束的也要算在金额内
        $res = $modelOrder->query("select sum(n_amt) as amt,sum(n_frozen_amt) as frozen_amt from __TABLE__ where uid=$uid and status>=2");
        if($res){
            $freeze_money = $res[0]['frozen_amt'];
            $cash_money = $res[0]['amt'];
        }else{
            $freeze_money = 0;
            $cash_money = 0;
        }
        $total_money = $freeze_money + $cash_money;
        
        //计算每天的基本奖励和环保奖励，仅在已生成保单的订单，不包括已经结束的
        $res = $modelOrder->query("select sum(day_amt) as amt,sum(day_frozen_amt) as frozen_amt from __TABLE__ where uid=$uid and status=2");
        if($res){
            $day_amt = $res[0]['amt'];
            $day_frozen_amt = $res[0]['frozen_amt'];
        }else{
            $day_amt = 0;
            $day_frozen_amt = 0;
        }
        
        //计算昨天的基本奖励和环保奖励
        $modelOrderAmt = M('OrderAmtLog');
        unset($where);
        $where['uid'] = $uid;
        $where['status'] = array('egt',2); //已支付且已经生成保单的订单,或过期的
        $subQuery = $modelOrder->field('order_id')->where($where)->select(false);
        
        $now = new \DateTime();
        $yesterday = $now->sub(new \DateInterval('P7D'));
        
        $chartLine = array();
        for($i=0;$i<7;$i++){            
            unset($where);
            $where['order_id'] = array('exp',"in $subQuery");
            $where['pdate'] = array('between',array($yesterday->format('Y-m-d'),$yesterday->format('Y-m-d 23:59:59.999999')));
            $where['isfrozen'] = 0;
            $where['ptype'] = 1;
            $resLog = $modelOrderAmt->where($where)->select();
            $arrOrder = array();
            $base_money = 0;
            foreach($resLog as $row){
                $arrOrder[] = $row['order_id'];
                $base_money += $row['chgval'];
            }
            $frozen_total = 0;
            $frozen_money = 0;
            if(!empty($arrOrder)){
                array_unique($arrOrder);
                unset($where);
                $where['order_id'] = array('in',$arrOrder);
                $frozen_total = $modelOrder->where($where)->sum('day_frozen_amt');
                $where['pdate'] = array('between',array($yesterday->format('Y-m-d'),$yesterday->format('Y-m-d 23:59:59.999999')));
                $where['isfrozen'] = 1;
                $where['ptype'] = 1;
                $frozen_money = $modelOrderAmt->where($where)->sum('chgval');
            }
            
            $line_data = array(
                'jb' => $base_money,
                'hb' => $frozen_money,
                'hbtotal' => $frozen_total,
                'hj' => $base_money+$frozen_money,
                'date' => $yesterday->format('m-d'),
            );
            $chartLine[] = $line_data;
            
            $yesterday = $yesterday->add(new \DateInterval('P1D'));
        } 
        
        $ret = array(
            'freeze_money' => $freeze_money,
            'cash_money' => $cash_money,
            'total_money' => $total_money,
            'day_amt' => $day_amt,
            'day_frozen_amt' => $day_frozen_amt,
            'data' => $chartLine,
        );
        $this->succ($ret);
    } 
    
    /**
    * 收支记录
    * 
    * @param mixed $p
    * @param mixed $limit
    * @param mixed $status
    * @param mixed $startdate
    * @param mixed $enddate
    */
    public function logs($p=1,$limit=10,$ptype='',$isfrozen='',$start_date='',$end_date=''){
        $uid = $this->uid;
        if(empty($uid)){
            $this->err('用户未登录!');
        }
        
        //获取兑换历史记录
        unset($where);
        $modelOrder = M('Order');
        $where['uid'] = $uid;
        $where['status'] = array('egt',2); //已支付且已经生成保单的订单,或过期的
        $subQuery = $modelOrder->field('order_id')->where($where)->select(false);
        
        unset($where);
        $where['order_id'] = array('exp',"IN $subQuery");
        if(empty($start_date)){
            if(empty($end_date)){
                
            }else{
                $end_date = date('Y-m-d',24*60*60 + strtotime($end_date));
                $where['pdate'] = array('elt',$end_date);
            }
        }else{
            if(empty($end_date)){
                $where['pdate'] = array('egt',$start_date);
            }else{
                $end_date = date('Y-m-d',24*60*60 + strtotime($end_date));
                $where['pdate'] = array('between',array($start_date,$end_date));
            }
        }
        if(!empty($ptype)){
            $where['ptype'] = $ptype;
        }
        if($isfrozen!=''){
            if(empty($isfrozen)){
                $where['isfrozen'] = 0;
            }else{
                $where['isfrozen'] = 1;
            }
        }
        
//        Log::record(print_r($_REQUEST,true));
//        Log::record(print_r($where,true));
        
        $modelOrderAmtLog = M('OrderAmtLog');
        $count = $modelOrderAmtLog->where($where)->count();
        $list = $modelOrderAmtLog->where($where)->order('pdate desc,pid desc')->page($p,$limit)->select();
        foreach($list as &$row){
            $resOrder = $modelOrder->cache(true)->where("order_id={$row['order_id']}")->find();
            if($resOrder){
                $row['car_no'] = $resOrder['car_no'];
            }
        }
        $ret = array(
            'count' => $count,
            'data' => $list,
        );
        $this->succ($ret);
   }
       
}